﻿#region License Revision: 0 Last Revised: 6/18/2008 11:00 AM
/******************************************************************************
Copyright (c) Microsoft Corporation.  All rights reserved.


This file is licensed under the Microsoft Public License (Ms-PL). A copy of the Ms-PL should accompany this file. 
If it does not, you can obtain a copy from: 

http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
******************************************************************************/
#endregion // License
using System;
using System.Windows.Browser;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
using System.Collections.ObjectModel;

namespace ScriptInterop
{
    public class JSArray<T> : JSObject
    {
        /// <summary>String holding JavaScript class name</summary>
        private static string thisClass = "Array";

        /// <summary>Get the JavaScript class name</summary>
        /// <returns type="String">The class name used in JavaScript </returns>		
        public override string ClassName
        {
            get { return thisClass; }
        }

        /// <summary>Construct a C# wrapper from a ScriptObject</summary>
        /// <param name="so">A ScriptObjet from JScript.</param>
        public JSArray(ScriptObject so) : base(so) { }

        /// <summary>
        /// Construct a JavaScript Array with an inital length set
        /// </summary>
        /// <param name="length">initial length of the array</param>
        public JSArray(int length)
        {
            ScriptObject = CreateClass();
            SetProperty("length", length);
        }

        /// <summary>
        /// Construct an empty JavaScript Array
        /// </summary>
        public JSArray()
        {
            ScriptObject = CreateClass();
            SetProperty("length", 0);
        }

        /// <summary>
        /// Length of the array (number of elements)
        /// </summary>
        public int Length
        {
            get { return GetProperty<int>("length"); }
        }

        /// <summary>
        /// Indexer that allows us to access like a normal array
        /// </summary>
        /// <param name="index">index to the element we want</param>
        /// <returns>an object of the appropriate type</returns>
        public T this[int index]	// Indexer declaration
        {
            get
            {
                // Check the index limits.
                if (index < 0 || index >= Length)
                    return default(T);
                else // Javascript arrays are just objects with numbered properties
                    return GetProperty<T>(index.ToString(CultureInfo.InvariantCulture));
            }
            set
            {
                if (index < 0)
                    return;
                if (index >= Length)
                {
                    SetProperty("length", index);
                }
                // Javascript arrays are just objects with numbered properties
                SetProperty(index.ToString(CultureInfo.InvariantCulture), value);
            }
        }
    }
}
